package edu.nf.ch08.web.api;

import edu.nf.ch08.common.ResultVO;
import edu.nf.ch08.enitiy.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author XCH
 * @date 2025 - 09 - 17
 */
@RestController
@RequestMapping("/api/v1")
//使用@Tag注解对当前的API进行描述
@Tag(name = "用户服务接口", description = "提供用户信息相关的API")
public class UserController {

    private final static List<User> users;
    static {
        User user1 = new User(1, "张三", 18);
        User user2 = new User(2, "李四", 18);
        User user3 = new User(3, "王五", 18);
        users = Arrays.asList(user1, user2, user3);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/user")
    @Operation(summary = "查询用户列表", description = "查询所有用户的基本信息", method = "GET")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务器错误")
    })
    public ResultVO<List<User>> listUsers() {
        return ResultVO.success(users);
    }

    @GetMapping("/user/{id}")
    // 方法描述
    @Operation(summary = "查询用户列表", description = "根据id查询用户信息", method = "GET")
    //参数描述
    @Parameters({
            //required属性表示必须传参
            //in表示
            @Parameter(name = "id", description = "用户id", required = true,in = ParameterIn.PATH)
    })
    //响应描述
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务器错误")
    })
    public ResultVO<User> getUser(@PathVariable("id") Integer uid) {
        for (User user : users) {
            if (user.getId().equals(uid)) {
                return ResultVO.success(user);
            }
        }
        return ResultVO.error(404, "用户不存在");
    }
    @PostMapping("/user")
    @Operation(summary = "添加用户", description = "添加用户信息", method = "POST")
    @Parameters({
            @Parameter(name = "user", required = true)
    })
    public ResultVO<String> addUser(@RequestBody User user) {
        return ResultVO.success();
    }
}
